package com.bwie.mq.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@Log4j2
public class SendConsumer {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    @RabbitListener(queuesToDeclare = @Queue("sendMessagee"))
    public void productUser(Message message, Channel channel){
        String messageId = message.getMessageProperties().getMessageId();
        Long add = redisTemplate.opsForSet().add("sendMessagee", messageId);
        if (add == 1) {
            log.info("MQ发送成功");
        }
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

    }
}
